# authot  wujehy

#　用于自动构建权限　模块的　脚本
# TODO 暂定
# [] 模块构建
# [] 权限管理器构建


import os
import sys
import platform


SYSTEMNAME = ''
PROJECT_HOME=os.getcwd()
Output_Path=PROJECT_HOME+"/Linux.out"
Config_DIR=Output_Path+"/config"
# openssl 放到 当前路径

BUILD_DIR=PROJECT_HOME + '/build'
GeeJoans_CONFIG_DIR=PROJECT_HOME+"/Geejoans"
GeeJoans_CONFIG_MQSERVER_DIR=GeeJoans_CONFIG_DIR+"/MQServer"

MQSERVER_BUILD_DIR=BUILD_DIR
MQSERVER_BUILD_SRC_DIR=PROJECT_HOME


def build_MQ_mosquitto():
    pass

def build_dir():
    # 创建 编译 目录
    if not os.path.exists(Output_Path + '/build'):
        os.makedirs(Output_Path + '/build')
    else:
        print("已存在 ",Output_Path + '/build' )


def output_config():
    # 输出配置文件
    pathCheck(Config_DIR)
    pathCheck(Config_DIR+"/conf.d")
    geejoans_conf="""
    pid_file /var/run/geejoans.pid\n
    persistence true\n
    persistence_location %s/\n
    log_dest file %s/geejoans.log\n
    include_dir %s/conf.d\n
    """%(Config_DIR,Config_DIR,Config_DIR)
    LIB_NAME = "so"
    print("SYSTEMNAME ", SYSTEMNAME)
    if 'Darwin' in  SYSTEMNAME:
        print("do Darwin")
        LIB_NAME = "dylib"
    elif 'Windows' in SYSTEMNAME :
        LIB_NAME = "lib"
        print("do Windows")
    else:
        print("linux ")
    print("LIB_NAME" , LIB_NAME)
    geejoans_conf_d="""
allow_anonymous false\n
auth_plugin %s/sbin/libmqtt_plugin.%s\n
auth_opt_db_path %s/test.db\n
    """%(Output_Path , LIB_NAME,Output_Path)

    with open(Config_DIR+'/MQconfig.conf','w') as f:
         f.write(geejoans_conf)
         f.close()

    with open(Config_DIR+'/conf.d/myconfig.conf','w') as f:
         f.write(geejoans_conf_d)
         f.close()

    with open(Config_DIR+'/geejoans.log','w') as f:
         f.write("# start")
         f.close()

def build_MQServer_core():
    pathCheck(MQSERVER_BUILD_DIR)
    os.chdir(MQSERVER_BUILD_DIR)
    MQServer_BUILD_CMD="cmake ..  && cmake --build . -- -j4 "
    print(MQServer_BUILD_CMD)
    os.system(MQServer_BUILD_CMD)
    os.chdir(PROJECT_HOME)

def Install_MQServer():
    os.chdir(MQSERVER_BUILD_DIR)
    MQServer_BUILD_CMD="cmake --install . "
    print(MQServer_BUILD_CMD)
    os.system(MQServer_BUILD_CMD)
    os.chdir(PROJECT_HOME)

def Run_SHELL():
# 输出运行文件
    geejoans_conf_run="""
#!/bin/bash
%s/sbin/mosquitto -c %s -v
    """%(Output_Path,Config_DIR+'/MQconfig.conf')

    geejoans_conf_Sub="""
#!/bin/bash
export LD_LIBRARY_PATH=%s
%s/bin/mosquitto_sub -u user1 -P password1  -t topic1 
    """%(Output_Path +"/lib", Output_Path)

    geejoans_conf_Pub="""
#!/bin/bash
export LD_LIBRARY_PATH=%s
%s/bin/mosquitto_pub  -u user1 -P password1  -t topic1  -m "test hello world"
    """%(Output_Path+"/lib" , Output_Path)
    # print(geejoans_conf_run)
    with open(Output_Path+'/testSub.sh','w') as f:
         f.write(geejoans_conf_Sub)
         f.close()

    with open(Output_Path+'/testPub.sh','w') as f:
         f.write(geejoans_conf_Pub)
         f.close()
    with open(Output_Path+'/run.sh','w') as f:
         f.write(geejoans_conf_run)
         f.close()
    pass

def pathCheck(dir):
    if not os.path.exists(dir):
            os.makedirs(dir)
    else:
        print("已存在 ",dir)

def main():
    global SYSTEMNAME
    SYSTEMNAME = platform.system()
    print(SYSTEMNAME)
    # 6. 清除编译
    while True:
        print("""欢迎使用 GeeJoans 自动编译系统
1.需要 基本条件，mysql 库 sudo apt install  libmysqlclient-dev 
2. cmake  automake gcc 等 基本 构建 工具
3. 编译输出 到 平台.out 目录 如 ：linux.out
操作菜单：
1. 全自动
2. 编译MQServer
3. 安装MQServer 和插件 
4. 生成默认配置文件
5. 生成测试脚本
q. exit
""")
        if len(sys.argv) >= 2:
            print("参数错误")
            break
        else:
            num = input()
            if num == "1":
                build_MQServer_core()
                build_MQServer_core()
                Install_MQServer()
                output_config()
                Run_SHELL()
                continue
            elif num == "2":
                build_MQServer_core()
                continue

            elif num == "3":
                Install_MQServer()

                continue
            elif num == "4":
                output_config()
                continue
            elif num=="5":
                Run_SHELL()
                continue
            elif num == "Q" or num =="q":
                print("exit")
                break
            else:
                print("参数错误")
                continue
if __name__=="__main__":
    # build_openssl()
    # build_auth_plug()
    # output_config()
    # build_MQServer_core()
    # Run_SHELL()
    main()


